//
//  UnbindViewController.m
//  Router
//
//  Created by teki on 15/10/14.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "UnbindViewController.h"
#import "SystemSettingHelper.h"
#import "UserSingleton.h"

#import "BindWIFIListViewController.h"
#import "AppDelegate.h"


@interface UnbindViewController ()<UIAlertViewDelegate>
@property (nonatomic, strong) SystemSettingHelper *systemHelper;
@end

@implementation UnbindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"解除绑定";
    [self initContentView];
    
    self.systemHelper = [[SystemSettingHelper alloc] init];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)initContentView{
    CGFloat padding = 28;
    CGFloat verticalPadding = 35;
    
    NSString *string = @"一旦解除绑定,你就不能通过小叶APP\n远程管理这个设备了";
    UILabel *label = [self.view addLabelWithText:string font:[UIFont systemFontOfSize:SIZE_TextTitleMini] color:SeparatorLineColor];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentCenter;
    [label makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(padding);
        make.centerX.equalTo(self.view);
    }];

    
    UIButton *assureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    assureBtn.translatesAutoresizingMaskIntoConstraints = NO;
    assureBtn.layer.cornerRadius = 5.0f;
    assureBtn.layer.masksToBounds = YES;
    [assureBtn setTitle:@"确认解除" forState:UIControlStateNormal];
    [assureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [assureBtn setBackgroundImage:[UIImage imageWithColor:NavgationBarBackgroundColor] forState:UIControlStateNormal];
    [assureBtn setBackgroundImage:[UIImage imageWithColor:SeparatorLineColor] forState:UIControlStateSelected];
    [assureBtn addTarget:self action:@selector(assureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:assureBtn];
    [assureBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(padding);
        make.top.equalTo(label.bottom).offset(verticalPadding);
        make.right.equalTo(-padding);
        make.height.equalTo(45);

    }];

}


#pragma mark -
#pragma mark   UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex ==1) {
        [self unBindRequest];
    }
}



#pragma mark -
#pragma mark   IBActions
- (void)assureButtonPressed:(UIButton *)sender
{
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"一旦解除绑定,你就不能通过小叶APP远程管理这个设备!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertView show];
}



#pragma mark -
#pragma mark   Requests


- (void)unBindRequest
{
    
    LoginResponseModel *model = [[UserSingleton shareInstance] userModel];
    WIFIModel *wifi = model.wifi;
    UserModel *user = model.user[0];
    self.activity = [self startActivityWithTitle:@"正在解除绑定..."];
    [self.systemHelper unBindUserAndDeviceWithDeviceID:wifi.device_id user:user.userName success:^(BOOL succ, id responseObject, NSString *msg) {
        if (responseObject)
        {
            NSArray *array = responseObject[@"bindDevice"];
            NSArray *bindDevics = [BindDeviceModel objectArrayWithKeyValuesArray:array];
            [UserSingleton shareInstance].userModel.bindDevice = bindDevics;
            [[UserSingleton shareInstance] saveData];
            [self stopActivityWithActivity:self.activity title:@"解绑成功!"];
            
            

             if ([array count]==0)
            {
                [self dispatchTimerWithTime:2.0 block:^{
                    BindWIFIListViewController *buynowVC = [[BindWIFIListViewController alloc] init];
                    BaseNavigationViewController *baseNav = [[BaseNavigationViewController alloc] initWithRootViewController:buynowVC];
                    ApplicationDelegate.window.rootViewController = baseNav;
                }];
            }
            else{
                [self dispatchTimerWithTime:2.0 block:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }

        }else
        {
            [self stopActivityWithActivity:self.activity title:@"解绑失败!"];
        }
        
        
    } failure:^(NSError *error) {
        NSString *failedReason = [error localizedDescription];
        [self stopActivityWithActivity:self.activity title:failedReason];
    }];

}
@end
